package cn.com.ayqu.module.erp.service.activityuser;

import cn.com.ayqu.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.com.ayqu.module.erp.controller.admin.activityuser.vo.*;
import cn.com.ayqu.module.erp.dal.dataobject.activityuser.ActivityUserDO;
import cn.com.ayqu.framework.common.pojo.PageResult;
import cn.com.ayqu.framework.common.util.object.BeanUtils;

import cn.com.ayqu.module.erp.dal.mysql.activityuser.ActivityUserMapper;

import static cn.com.ayqu.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.com.ayqu.module.erp.enums.ErrorCodeConstants.*;

/**
 * 用户参与活动 Service 实现类
 *
 * @author 高连杰
 */
@Service
@Validated
public class ActivityUserServiceImpl implements ActivityUserService {

    @Resource
    private ActivityUserMapper activityUserMapper;

    @Override
    public Long createActivityUser(ActivityUserSaveReqVO createReqVO) {
        // 插入
        ActivityUserDO activityUser = BeanUtils.toBean(createReqVO, ActivityUserDO.class);
        activityUserMapper.insert(activityUser);
        // 返回
        return activityUser.getId();
    }

    @Override
    public void updateActivityUser(ActivityUserSaveReqVO updateReqVO) {
        // 校验存在
        validateActivityUserExists(updateReqVO.getId());
        // 更新
        ActivityUserDO updateObj = BeanUtils.toBean(updateReqVO, ActivityUserDO.class);
        activityUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivityUser(Long id) {
        // 校验存在
        validateActivityUserExists(id);
        // 删除
        activityUserMapper.deleteById(id);
    }

    private void validateActivityUserExists(Long id) {
        if (activityUserMapper.selectById(id) == null) {
            throw exception(ACTIVITY_USER_NOT_EXISTS);
        }
    }

    @Override
    public PageResult<ActivityUserDO> getActivityUser(ActivityUserDetailPageReqVO pageReqVO) {
        return activityUserMapper.selectPage(pageReqVO, new LambdaQueryWrapperX<ActivityUserDO>()
                .eq(ActivityUserDO::getActivityId, pageReqVO.getActivityId())
                .eq(ActivityUserDO::getUserId, pageReqVO.getUserId())
        );
    }

    @Override
    public PageResult<ActivityUserDO> getActivityUserPage(ActivityUserPageReqVO pageReqVO) {
        return activityUserMapper.selectPage(pageReqVO);
    }

    @Override
    public ActivityUserDO getActivityUserJoinInfo(Long id) {
        return activityUserMapper.selectById(id);
    }

}